//index.js
import config from '../../config.js';
const base = config;

//获取应用实例
const app = getApp();
Page({
  data: {

    //车辆列表
    carList: [],
    mainInfo: {},
    // 我的管家
    manager: {
      header: "../../image/default.png",
      name: "张伟",
      score: "9.0",
      level: 4.5,
      adept: "汽车挂牌；车辆保养、维修；续保、保险理赔等",
      detail: "../managerDetail/managerDetail"
    },
    carId: null

  },
  //违章查询
  illegalQuery: function(e) {
    let id = this.data.carId;
    if (id != null) {
      wx.navigateTo({
        url: '../peccancyList/peccancyList?id=' + id,
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请先选择要查询的车辆',
      })
    }
  },

  onLoad: function() {
    this.getCarList();
  },
  onShow: function() {

  },
  getCarList: function() {
    var that = this;
    wx.showLoading({
      title: '查询中...',
    })
    //请求车辆列表
    wx.request({
      url: base.myCarsList,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: 0,
        type:1
      },
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200) {
          that.setData({
            carList: res.data.data
          })

        } else {
          wx.showToast({
            title: '车辆列表获取失败',
            icon: 'none'
          })
        }

        wx.hideLoading()
      }
    })
  },
  agreementDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    var url = config.http + "://" + config.host + "/agreemmentContent.action?contentId=" + id;
    wx.setStorageSync("webviewUrl", url);
    wx.navigateTo({
      url: "../webview/webview"
    })
  },
  setDefault: function(e) {
    console.log(e);
    var carId = e.detail.value;
    this.setData({
      carId: carId
    })
    /**var that = this;

    //设置默认
    wx.request({
      url: base.defaultCar,
      header: {
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: carId
      },
      success: function(res) {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            carList: res.data.data
          })
        }
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })**/
  },
  //删除车辆
  deleteCar: function(e) {
    var instanceOk = this;
    wx.showModal({
      title: "温馨提示",
      content: "是否确认删除车辆信息？",
      showCancel: true,
      confirmColor: "#137AF0",
      success: function(er) {
        console.log(er);
        if (er.confirm) {
          let id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index,
            that = this;
          wx.showLoading({
            title: '正在删除',
          })
          wx.request({
            url: base.carDelete,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': wx.getStorageSync("session")
            },
            data: {
              id: id
            },
            success: function(res) {
              wx.hideLoading();
              console.log(res);
              if (res.data.status == 200) {
                wx.showToast({
                  title: '删除成功',
                })
                instanceOk.getCarList();
              } else {
                wx.showToast({
                  title: "删除失败",
                  icon: "none"
                })
              }
            }
          })
        }
      }
    })
  },
})